/*
 * Copyright (c) 2020. André Mion
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.andremion.louvre.util;

import com.andremion.louvre.ResourceTable;

import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.io.IOException;

public class AnimationHelper {
    private static final HiLogLabel TAG = new HiLogLabel(3, 0xD000F00, AnimationHelper.class.getSimpleName());

    public static void scaleView(Component component, float scale) {
        if (component.getScaleX() != scale || component.getScaleY() != scale) {
            long duration = getInteger(component.getContext(), ResourceTable.Integer_config_shortAnimTime);
            AnimatorProperty animatorProperty = component.createAnimatorProperty();
            animatorProperty.scaleX(scale);
            animatorProperty.scaleY(scale);
            animatorProperty.setDuration(duration);
            animatorProperty.start();
        }
    }

    /**
     * getInteger
     *
     * @param context the context
     * @param id      the string id
     * @return string of the given id
     */
    public static int getInteger(Context context, int id) {
        int result = 0;
        if (context == null) {
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            return result;
        }
        try {
            result = manager.getElement(id).getInteger();
        } catch (IOException e) {
            HiLog.error(TAG, "getInteger -> IOException");
        } catch (NotExistException e) {
            HiLog.error(TAG, "getInteger -> NotExistException");
        } catch (WrongTypeException e) {
            HiLog.error(TAG, "getInteger -> WrongTypeException");
        }
        return result;
    }

}
